package com.example.demo.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.common.Result;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user){
        System.out.println(user);
        return  userService.userLogin(user);
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result<?> register(@RequestBody User user){
        if (user.getPassword() == null){
            user.setPassword("123456");
        }
        return  userService.userRegister(user);
    }

    /**
     * 分页查询用户
     * @param pageNum
     * @param pageSize
     * @param nickName
     * @param sex
     * @return
     */
    @GetMapping("/selectUser")
    public Result<?> selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                @RequestParam(defaultValue = "10") Integer pageSize,
                                @RequestParam(defaultValue = "") String nickName,
                                @RequestParam(defaultValue = "") String username,
                                @RequestParam(defaultValue = "") String sex,
                                @RequestParam(defaultValue = "") String phoneNumber){
        IPage<User> userPage = userService.selectPage(pageNum,pageSize,nickName,username,sex,phoneNumber);
        return Result.success(userPage);
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public Result<?> save(@RequestBody User user){
        if (user.getPassword() == null){
            user.setPassword("123456");
        }
        userService.addUser(user);
        return Result.success();
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @PutMapping("/updateUser")
    public Result<?> update(@RequestBody User user){
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @GetMapping("/deleteUser")
    public Result<?> delete(@RequestParam int userId){
        userService.deleteUser(userId);
        return  Result.success();
    }

    /**
     * 查询用户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable int id) {
        User user = userService.selectById(id);
        return Result.success(user);
    }

    /**
     * 根据角色编码查询用户
     * @param roleId
     * @return
     */
    @GetMapping("/selectUserByRoleCode")
    public Result<?> selectUserByRoleCode(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                          @RequestParam(defaultValue = "") String roleId,
                                          @RequestParam(defaultValue = "") String phoneNumber,
                                          @RequestParam(defaultValue = "") String username){
        IPage<User> userPageList = userService.selectUserRole(pageNum,pageSize,roleId,phoneNumber,username);
        return  Result.success(userPageList);
    }
}
